<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>directtransport_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_xpc_directtransport_test.js.html">directtransport_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Tests the direct transport.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.net.xpc.DirectTransportTest&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.dom&#39;);
<a name="line22"></a>goog.require(&#39;goog.log&#39;);
<a name="line23"></a>goog.require(&#39;goog.log.Level&#39;);
<a name="line24"></a>goog.require(&#39;goog.net.xpc&#39;);
<a name="line25"></a>goog.require(&#39;goog.net.xpc.CfgFields&#39;);
<a name="line26"></a>goog.require(&#39;goog.net.xpc.CrossPageChannel&#39;);
<a name="line27"></a>goog.require(&#39;goog.net.xpc.CrossPageChannelRole&#39;);
<a name="line28"></a>goog.require(&#39;goog.net.xpc.TransportTypes&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line31"></a>goog.setTestOnly(&#39;goog.net.xpc.DirectTransportTest&#39;);
<a name="line32"></a>
<a name="line33"></a>var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(
<a name="line34"></a>    &#39;Direct transport tests.&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Echo service name.
<a name="line39"></a> * @type {string}
<a name="line40"></a> * @const
<a name="line41"></a> */
<a name="line42"></a>var ECHO_SERVICE_NAME = &#39;echo&#39;;
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * Response service name.
<a name="line47"></a> * @type {string}
<a name="line48"></a> * @const
<a name="line49"></a> */
<a name="line50"></a>var RESPONSE_SERVICE_NAME = &#39;response&#39;;
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Test Payload.
<a name="line55"></a> * @type {string}
<a name="line56"></a> * @const
<a name="line57"></a> */
<a name="line58"></a>var MESSAGE_PAYLOAD_1 = &#39;This is message payload 1.&#39;;
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>/**
<a name="line62"></a> * The name id of the peer iframe.
<a name="line63"></a> * @type {string}
<a name="line64"></a> * @const
<a name="line65"></a> */
<a name="line66"></a>var PEER_IFRAME_ID = &#39;peer-iframe&#39;;
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>// Class aliases.
<a name="line70"></a>var CfgFields;
<a name="line71"></a>var CrossPageChannel;
<a name="line72"></a>var CrossPageChannelRole;
<a name="line73"></a>var TransportTypes;
<a name="line74"></a>
<a name="line75"></a>var outerXpc;
<a name="line76"></a>var innerXpc;
<a name="line77"></a>var peerIframe;
<a name="line78"></a>var channelName;
<a name="line79"></a>var messageIsSync = false;
<a name="line80"></a>
<a name="line81"></a>function setUpPage() {
<a name="line82"></a>  CfgFields = goog.net.xpc.CfgFields;
<a name="line83"></a>  CrossPageChannel = goog.net.xpc.CrossPageChannel;
<a name="line84"></a>  CrossPageChannelRole = goog.net.xpc.CrossPageChannelRole;
<a name="line85"></a>  TransportTypes = goog.net.xpc.TransportTypes;
<a name="line86"></a>
<a name="line87"></a>  // Show debug log
<a name="line88"></a>  var debugDiv = document.createElement(&#39;debugDiv&#39;);
<a name="line89"></a>  document.body.appendChild(debugDiv);
<a name="line90"></a>  var logger = goog.log.getLogger(&#39;goog.net.xpc&#39;);
<a name="line91"></a>  logger.setLevel(goog.log.Level.ALL);
<a name="line92"></a>  goog.log.addHandler(logger, function(logRecord) {
<a name="line93"></a>    var msgElm = goog.dom.createDom(&#39;div&#39;);
<a name="line94"></a>    msgElm.innerHTML = logRecord.getMessage();
<a name="line95"></a>    goog.dom.appendChild(debugDiv, msgElm);
<a name="line96"></a>  });
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>function tearDown() {
<a name="line101"></a>  if (peerIframe) {
<a name="line102"></a>    document.body.removeChild(peerIframe);
<a name="line103"></a>    peerIframe = null;
<a name="line104"></a>  }
<a name="line105"></a>  if (outerXpc) {
<a name="line106"></a>    outerXpc.dispose();
<a name="line107"></a>    outerXpc = null;
<a name="line108"></a>  }
<a name="line109"></a>  if (innerXpc) {
<a name="line110"></a>    innerXpc.dispose();
<a name="line111"></a>    innerXpc = null;
<a name="line112"></a>  }
<a name="line113"></a>  window.iframeLoadHandler = null;
<a name="line114"></a>  channelName = null;
<a name="line115"></a>  messageIsSync = false;
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>function createIframe() {
<a name="line120"></a>  peerIframe = document.createElement(&#39;iframe&#39;);
<a name="line121"></a>  peerIframe.id = PEER_IFRAME_ID;
<a name="line122"></a>  document.body.insertBefore(peerIframe, document.body.firstChild);
<a name="line123"></a>}
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Tests 2 same domain frames using direct transport.
<a name="line128"></a> */
<a name="line129"></a>function testDirectTransport() {
<a name="line130"></a>  createIframe();
<a name="line131"></a>  channelName = goog.net.xpc.getRandomString(10);
<a name="line132"></a>  outerXpc = new CrossPageChannel(
<a name="line133"></a>      getConfiguration(CrossPageChannelRole.OUTER, PEER_IFRAME_ID));
<a name="line134"></a>  // Outgoing service.
<a name="line135"></a>  outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction);
<a name="line136"></a>  // Incoming service.
<a name="line137"></a>  outerXpc.registerService(
<a name="line138"></a>      RESPONSE_SERVICE_NAME,
<a name="line139"></a>      responseMessageHandler_testDirectTransport);
<a name="line140"></a>  asyncTestCase.waitForAsync(&#39;Waiting for xpc connect.&#39;);
<a name="line141"></a>  outerXpc.connect(onConnect_testDirectTransport);
<a name="line142"></a>  // inner_peer.html calls this method at end of html.
<a name="line143"></a>  window.iframeLoadHandler = onIframeLoaded_testDirectTransport;
<a name="line144"></a>  peerIframe.src = &#39;testdata/inner_peer.html&#39;;
<a name="line145"></a>}
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>function onIframeLoaded_testDirectTransport() {
<a name="line149"></a>  peerIframe.contentWindow.instantiateChannel(
<a name="line150"></a>      getConfiguration(CrossPageChannelRole.INNER));
<a name="line151"></a>}
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>function onConnect_testDirectTransport() {
<a name="line155"></a>  assertTrue(&#39;XPC over direct channel is connected&#39;, outerXpc.isConnected());
<a name="line156"></a>  outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1);
<a name="line157"></a>}
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>function responseMessageHandler_testDirectTransport(message) {
<a name="line161"></a>  assertEquals(
<a name="line162"></a>      &#39;Received payload is equal to sent payload.&#39;,
<a name="line163"></a>      message,
<a name="line164"></a>      MESSAGE_PAYLOAD_1);
<a name="line165"></a>  asyncTestCase.continueTesting();
<a name="line166"></a>}
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * Tests 2 xpc&#39;s communicating with each other in the same window.
<a name="line171"></a> */
<a name="line172"></a>function testSameWindowDirectTransport() {
<a name="line173"></a>  channelName = goog.net.xpc.getRandomString(10);
<a name="line174"></a>
<a name="line175"></a>  outerXpc = new CrossPageChannel(getConfiguration(CrossPageChannelRole.OUTER));
<a name="line176"></a>  outerXpc.setPeerWindowObject(self);
<a name="line177"></a>
<a name="line178"></a>  // Outgoing service.
<a name="line179"></a>  outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction);
<a name="line180"></a>  // Incoming service.
<a name="line181"></a>  outerXpc.registerService(
<a name="line182"></a>      RESPONSE_SERVICE_NAME,
<a name="line183"></a>      outerResponseMessageHandler_testSameWindowDirectTransport);
<a name="line184"></a>  asyncTestCase.waitForAsync(&#39;Waiting for outer xpc connect.&#39;);
<a name="line185"></a>  outerXpc.connect(onOuterConnect_testSameWindowDirectTransport);
<a name="line186"></a>
<a name="line187"></a>  innerXpc = new CrossPageChannel(getConfiguration(CrossPageChannelRole.INNER));
<a name="line188"></a>  innerXpc.setPeerWindowObject(self);
<a name="line189"></a>  // Incoming service.
<a name="line190"></a>  innerXpc.registerService(
<a name="line191"></a>      ECHO_SERVICE_NAME,
<a name="line192"></a>      innerEchoMessageHandler_testSameWindowDirectTransport);
<a name="line193"></a>  // Outgoing service.
<a name="line194"></a>  innerXpc.registerService(
<a name="line195"></a>      RESPONSE_SERVICE_NAME,
<a name="line196"></a>      goog.nullFunction);
<a name="line197"></a>  innerXpc.connect();
<a name="line198"></a>}
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>function onOuterConnect_testSameWindowDirectTransport() {
<a name="line202"></a>  assertTrue(
<a name="line203"></a>      &#39;XPC over direct channel, same window, is connected&#39;,
<a name="line204"></a>      outerXpc.isConnected());
<a name="line205"></a>  outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1);
<a name="line206"></a>}
<a name="line207"></a>
<a name="line208"></a>
<a name="line209"></a>function outerResponseMessageHandler_testSameWindowDirectTransport(message) {
<a name="line210"></a>  assertEquals(
<a name="line211"></a>      &#39;Received payload is equal to sent payload.&#39;,
<a name="line212"></a>      message,
<a name="line213"></a>      MESSAGE_PAYLOAD_1);
<a name="line214"></a>  asyncTestCase.continueTesting();
<a name="line215"></a>}
<a name="line216"></a>
<a name="line217"></a>
<a name="line218"></a>function innerEchoMessageHandler_testSameWindowDirectTransport(message) {
<a name="line219"></a>  innerXpc.send(RESPONSE_SERVICE_NAME, message);
<a name="line220"></a>}
<a name="line221"></a>
<a name="line222"></a>
<a name="line223"></a>function getConfiguration(role, opt_peerFrameId) {
<a name="line224"></a>  var cfg = {};
<a name="line225"></a>  cfg[CfgFields.TRANSPORT] = TransportTypes.DIRECT;
<a name="line226"></a>  if (goog.isDefAndNotNull(opt_peerFrameId)) {
<a name="line227"></a>    cfg[CfgFields.IFRAME_ID] = opt_peerFrameId;
<a name="line228"></a>  }
<a name="line229"></a>  cfg[CfgFields.CHANNEL_NAME] = channelName;
<a name="line230"></a>  cfg[CfgFields.ROLE] = role;
<a name="line231"></a>  return cfg;
<a name="line232"></a>}
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/**
<a name="line236"></a> * Tests 2 same domain frames using direct transport using sync mode.
<a name="line237"></a> */
<a name="line238"></a>function testSyncMode() {
<a name="line239"></a>  createIframe();
<a name="line240"></a>  channelName = goog.net.xpc.getRandomString(10);
<a name="line241"></a>
<a name="line242"></a>  var cfg = getConfiguration(CrossPageChannelRole.OUTER, PEER_IFRAME_ID);
<a name="line243"></a>  cfg[CfgFields.DIRECT_TRANSPORT_SYNC_MODE] = true;
<a name="line244"></a>
<a name="line245"></a>  outerXpc = new CrossPageChannel(cfg);
<a name="line246"></a>  // Outgoing service.
<a name="line247"></a>  outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction);
<a name="line248"></a>  // Incoming service.
<a name="line249"></a>  outerXpc.registerService(
<a name="line250"></a>      RESPONSE_SERVICE_NAME,
<a name="line251"></a>      responseMessageHandler_testSyncMode);
<a name="line252"></a>  asyncTestCase.waitForAsync(&#39;Waiting for xpc connect.&#39;);
<a name="line253"></a>  outerXpc.connect(onConnect_testSyncMode);
<a name="line254"></a>  // inner_peer.html calls this method at end of html.
<a name="line255"></a>  window.iframeLoadHandler = onIframeLoaded_testSyncMode;
<a name="line256"></a>  peerIframe.src = &#39;testdata/inner_peer.html&#39;;
<a name="line257"></a>}
<a name="line258"></a>
<a name="line259"></a>
<a name="line260"></a>function onIframeLoaded_testSyncMode() {
<a name="line261"></a>  var cfg = getConfiguration(CrossPageChannelRole.INNER);
<a name="line262"></a>  cfg[CfgFields.DIRECT_TRANSPORT_SYNC_MODE] = true;
<a name="line263"></a>  peerIframe.contentWindow.instantiateChannel(cfg);
<a name="line264"></a>}
<a name="line265"></a>
<a name="line266"></a>
<a name="line267"></a>function onConnect_testSyncMode() {
<a name="line268"></a>  assertTrue(&#39;XPC over direct channel is connected&#39;, outerXpc.isConnected());
<a name="line269"></a>  messageIsSync = true;
<a name="line270"></a>  outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1);
<a name="line271"></a>  messageIsSync = false;
<a name="line272"></a>}
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>function responseMessageHandler_testSyncMode(message) {
<a name="line276"></a>  assertTrue(&#39;The message response was syncronous&#39;, messageIsSync);
<a name="line277"></a>  assertEquals(
<a name="line278"></a>      &#39;Received payload is equal to sent payload.&#39;,
<a name="line279"></a>      message,
<a name="line280"></a>      MESSAGE_PAYLOAD_1);
<a name="line281"></a>  asyncTestCase.continueTesting();
<a name="line282"></a>}
</pre>


</body>
</html>
